import time

import cv2
import numpy as np

# 1. 截图保存

import uiautomator2 as u2
device_serial = "ad2f78b5"
d = u2.connect(device_serial)
d.screenshot("screen.png")

# 2. 读取模板图片（提前截取目标按钮的小图）
template = cv2.imread("D:\pythonProject\project1\start_Scrcpy\img_1.png", 0)
screen = cv2.imread("screen.png", 0)

# 3. 模板匹配
result = cv2.matchTemplate(screen, template, cv2.TM_CCOEFF_NORMED)
_, _, _, max_loc = cv2.minMaxLoc(result)

# 4. 计算中心点
w, h = template.shape[::-1]
center_x = max_loc[0] + w // 2
center_y = max_loc[1] + h // 2

# 5. 点击
for i in range(5):
     d.click(center_x, center_y)
     time.sleep(1)
print(center_x, center_y)